package com.fifth.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.*;
import com.fifth.home.mapper.BottomArticleMapper;
import com.fifth.home.service.IBottomArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@Service
public class BottomArticleServiceImpl extends ServiceImpl<BottomArticleMapper, BottomArticle> implements IBottomArticleService {
    @Resource
    private BottomArticleMapper bottomArticleMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 后台查询所有底部文章
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public CommonResult selectAllBottomArticle(Integer currentPage, Integer pageSize) {
        IPage<BottomArticle> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted","0");
        wrapper.eq("parent_id","0");
        //一级菜单分页
        IPage<BottomArticle> articleIPage = bottomArticleMapper.selectPage(page,wrapper);
        QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
        //查看所有数据
        List<BottomArticle> bottomArticles = bottomArticleMapper.selectList(wrapper1);
        System.out.println(bottomArticles);

        //设置层级关系

        //为一级菜单设置二级菜单
        for (BottomArticle first : articleIPage.getRecords()) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            first.setChildren(findChildren(bottomArticles, first.getId()));

        }
        return new CommonResult(2000,"查询底部文章成功",articleIPage);
    }

    /**
     * 模糊查询
     * @param currentPage
     * @param pageSize
     * @param bottomArticle
     * @return
     */
    @Override
    public CommonResult selectAllfurryBottomArticle(Integer currentPage, Integer pageSize, BottomArticle bottomArticle) {
        IPage<BottomArticle> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted","0");
        wrapper.like(StringUtils.isNotBlank(bottomArticle.getName()),"name",bottomArticle.getName());
        wrapper.eq(StringUtils.isNotBlank(bottomArticle.getIsDisable()),"is_disable",bottomArticle.getIsDisable());

        IPage<BottomArticle> articleIPage = bottomArticleMapper.selectPage(page,wrapper);
        QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();


        return new CommonResult(2000,"查询底部文章成功",articleIPage);
    }

    /**
     * 后台添加
     * @param bottomArticle
     * @return
     */
    @Override
    @Transactional
    public CommonResult insertBottomArticle(BottomArticle bottomArticle) {
        int insert = bottomArticleMapper.insert(bottomArticle);

        String bottomArticle1 = redisTemplate.opsForValue().get("bottomArticle");
        if(bottomArticle1!=null && !"".equals(bottomArticle1)){
            Boolean head1 = redisTemplate.delete("bottomArticle");
        }
        return new CommonResult(2000,"添加底部文章成功",null);
    }

    /**
     * 修改文章状态
     * @param id
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult updateStatus(String id, String isDisable) {

        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        BottomArticle bottomArticle = new BottomArticle();
        bottomArticle.setIsDisable(isDisable);
        int update = bottomArticleMapper.update(bottomArticle, wrapper);

        String bottomArticle1 = redisTemplate.opsForValue().get("bottomArticle");
        if(bottomArticle1!=null && !"".equals(bottomArticle1)){
            //删除redis数据
            Boolean head1 = redisTemplate.delete("bottomArticle");
        }
        return new CommonResult(2000, "修改状态成功", null);
    }

    /**
     * 删除底部文章
     * @param id
     * @return
     */
    @Override
    public CommonResult removeBottomArticle(String id) {
        int i = bottomArticleMapper.deleteById(id);

        String bottomArticle = redisTemplate.opsForValue().get("bottomArticle");
        if(bottomArticle!=null && !"".equals(bottomArticle)){
            Boolean bottomArticle1 = redisTemplate.delete("bottomArticle");
        }
        return new CommonResult(2000, "删除底部文章成功", null);
    }

    /**
     * 修改底部文章
     * @param bottomArticle
     * @return
     */
    @Override
    public CommonResult updateBottomArticle(BottomArticle bottomArticle) {
        bottomArticle.setGmtModified(null);
        int update = bottomArticleMapper.updateById(bottomArticle);

        String bottomArticle1 = redisTemplate.opsForValue().get("bottomArticle");
        if(bottomArticle1!=null && !"".equals(bottomArticle1)){
            Boolean banner2 = redisTemplate.delete("bottomArticle");
        }
        return new CommonResult(2000, "修改底部文章标题成功", null);
    }

    /**
     * 查询所有底部文章 (前端调用)
     * @return
     */
    @Override
    public CommonResult selectQianBottomArticle() {
        if (redisTemplate.hasKey("bottomArticle")) {
            //查看redis是否存在bottomArticle
            List<BottomArticle> list = JSON.parseObject(redisTemplate.opsForValue().get("bottomArticle"), List.class);
            return new CommonResult(2000, "底部文章查询成功", list);

        } else {

            QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
            wrapper.eq("is_deleted","0");
            wrapper.eq("is_disable","0");
            wrapper.eq("parent_id","0");
            //一级菜单
            List<BottomArticle> bottomArticles = bottomArticleMapper.selectList(wrapper);

            QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("is_deleted","0");
            wrapper1.eq("is_disable","0");
            //查看所有数据
            List<BottomArticle> bottomArticlesAll = bottomArticleMapper.selectList(wrapper1);
            System.out.println(bottomArticles);

            //设置层级关系

            //为一级菜单设置二级菜单
            for (BottomArticle first : bottomArticles) {
                //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
                first.setChildren(findChildren(bottomArticlesAll, first.getId()));

            }

            //转为字符串
            String s = JSON.toJSONString(bottomArticles);
            //System.out.println(s);
            redisTemplate.opsForValue().set("bottomArticle", s,24, TimeUnit.HOURS);
            return new CommonResult(2000,"查询底部文章成功",bottomArticles);
        }
    }


    //方法递归
    private List<BottomArticle> findChildren(List<BottomArticle> articleIPage, String id) {
        List<BottomArticle> children = new ArrayList<>();
        for (BottomArticle p : articleIPage) {
            if (p.getParentId().equals(id)) {
                children.add(p);
            }
        }
        for (BottomArticle child : children) {
            child.setChildren(findChildren(articleIPage, child.getId()));
        }
        return children;
    }
}
